package gold.contest;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC5815 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 带优化的dp问题，很经典的解法和问题：https://leetcode-cn.com/problems/maximum-number-of-points-with-cost/solution/kou-fen-hou-de-zui-da-de-fen-by-leetcode-60zl/
     * @return:
     */
    class Solution {
        public long maxPoints(int[][] points) {
            int m = points.length;
            int n = points[0].length;
            long[] currRow = new long[n];
            for (int j = 0; j < n; j++) {
                currRow[j] = points[0][j];
            }
            long[] maxLeft = new long[n]; // points[i][j] + j
            long[] maxRight = new long[n]; // points[i][j] - j

            for (int i = 1; i < m; i++) {
                maxLeft[0] = currRow[0];
                for (int j = 1; j < n; j++) {
                    maxLeft[j] = Math.max(maxLeft[j - 1], currRow[j] + j);
                }
                maxRight[n - 1] = currRow[n - 1] - (n - 1);
                for (int j = n - 2; j >= 0; j--) {
                    maxRight[j] = Math.max(maxRight[j + 1], currRow[j] - j);
                }
                for (int j = 0; j < n; j++) {
                    currRow[j] = Math.max(points[i][j] + j + maxRight[j], points[i][j] - j + maxLeft[j]);
                }
            }
            return Arrays.stream(currRow).max().getAsLong();
        }
    }


    class Solution_Time_Exceed {
        Map<String, Long> memo = new HashMap<>();
        Map<String, Long> secondMemo = new HashMap<>();

        public long maxPoints(int[][] points) {
            long curMaxScore = Integer.MIN_VALUE;
            for (int i = 0; i < points[0].length; i++) {
                long curScore = points[0][i] + maxPointsDFS(points, 1, i);
                curMaxScore = Math.max(curMaxScore, curScore);
            }

            return curMaxScore;
        }

        //int row 是当前出发的起始行
        public long maxPointsDFS(int[][] points, int curRow, int lastCol) {
            if (curRow == points.length) return 0;
            String key = curRow + "_" + lastCol;
            if (memo.containsKey(key)) return memo.get(key);

            long curMaxScore = Integer.MIN_VALUE;
            for (int i = 0; i < points[curRow].length; i++) {
                if (points[curRow][i] < Math.abs(i - lastCol)) continue;

                long curScore = 0;
                String secondKey = curRow + "_" + i + "_" + Math.abs(i - lastCol);
                if (secondMemo.containsKey(secondKey)) {
                    curScore = secondMemo.get(secondKey);
                } else {
                    curScore = points[curRow][i] - Math.abs(i - lastCol) + maxPointsDFS(points, curRow + 1, i);
                    secondMemo.put(secondKey, curScore);
                }

                curMaxScore = Math.max(curMaxScore, curScore);
            }

            memo.put(key, curMaxScore);
            return curMaxScore;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[][] arr = InputUtil.toDoubleIntegerArray("[[1,2,3],[1,5,1],[3,1,1]]");
        System.out.println(solution.maxPoints(arr));
    }

    public static void main(String[] args) throws Exception {
        LC5815 an = new LC5815();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
